#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 405;
int n, val[N], b[12], ans;
vector<int> a[N]; 
bool vis[12];
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f*=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
inline void sub1(int now){
	if(now == n + 1){
		int cnt = 0;
		int c[12];
		for(int i = 1; i <= n; i++) c[i] = val[i];
		for(int i = 1; i <= n; i++){
			cnt += c[b[i]];
			for(int j = 0; j < a[b[i]].size(); j++) c[a[b[i]][j]] += c[b[i]]; 
		}
		ans = max(ans, cnt);
		return;
	}
	for(int i = 1; i <= n; i++){
		if(!vis[i]){
			vis[i] = 1;
			b[now] = i;
			sub1(now + 1);
			vis[i] = 0;	
		}
	}
}
signed main(){
	freopen("light.in", "r",stdin);
	freopen("light.out", "w",stdout);
	n = read();
	for(int i = 1; i <= n; i++) val[i] = read();
	for(int i = 1, u, v; i < n; i++){
		u = read(), v = read();
		a[u].push_back(v), a[v].push_back(u);
	}
	if(n <= 10){
		ans = 0, sub1(1);
		cout << ans << endl;
	}
	return 0;
}
/*
5
1 2 3 4 5
1 2
1 3
2 4
2 5
*/
